import { PlanType, Status } from './base';

// 简化的功能特性（用于模拟数据）
export interface PlanFeature {
  key: string;
  name: string;
}

// 简化的订阅套餐（用于模拟数据和API）
export interface SubscriptionPlan {
  id: string;
  name: string;
  price: number;
  billingCycle: 'monthly' | 'yearly';
  maxUsers: number;
  maxOrganizations: number;
  maxStorage: number;
  features?: PlanFeature[];
  description?: string;
  isPopular?: boolean;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

// 完整的功能特性
export interface DetailedPlanFeature {
  code: string;
  name: string;
  description?: string;
  enabled: boolean;
  config?: any; // 功能配置参数
}

// 套餐限制
export interface PlanLimits {
  maxUsers: number;
  maxOrganizations: number;
  maxDepartments: number;
  maxStorage: number; // MB
  maxApiCalls: number; // 每月API调用次数
  maxConcurrentUsers: number;
  maxDataRetentionDays: number;
  maxCustomFields: number;
  allowCrossTenant: boolean;
  allowCustomDomain: boolean;
}

// 套餐价格
export interface PlanPricing {
  currency: string;
  billingCycle: 'monthly' | 'quarterly' | 'yearly';
  unitPrice: number; // 单价
  setupFee?: number; // 开通费
  discountRate?: number; // 折扣率
}

// 订阅套餐实体
export interface DetailedSubscriptionPlan {
  id: string;
  name: string;
  code: string;
  type: PlanType;
  description?: string;
  features: DetailedPlanFeature[];
  limits: PlanLimits;
  pricing: PlanPricing[];
  status: Status;
  isDefault: boolean;
  isVisible: boolean;
  displayOrder: number;
  validFrom?: string;
  validTo?: string;
  createdAt: string;
  updatedAt: string;
  createdBy?: string;
  updatedBy?: string;
  // 计算属性
  subscriptionCount?: number;
  isActive?: boolean;
}

// 租户订阅
export interface TenantSubscription {
  id: string;
  tenantId: string;
  planId: string;
  status: 'active' | 'expired' | 'trial' | 'cancelled' | 'suspended';
  startDate: string;
  endDate: string;
  trialEndDate?: string;
  autoRenew: boolean;
  paymentStatus: 'paid' | 'pending' | 'failed' | 'refunded';
  billingCycle: 'monthly' | 'quarterly' | 'yearly';
  unitPrice: number;
  quantity: number; // 购买数量（用户数等）
  totalAmount: number;
  currency: string;
  createdAt: string;
  updatedAt: string;
  // 关联信息
  plan?: SubscriptionPlan;
  tenant?: {
    id: string;
    name: string;
    code: string;
  };
}

// 组织订阅
export interface OrganizationSubscription {
  id: string;
  organizationId: string;
  planId: string;
  status: 'active' | 'expired' | 'trial' | 'cancelled' | 'suspended';
  startDate: string;
  endDate: string;
  trialEndDate?: string;
  autoRenew: boolean;
  paymentStatus: 'paid' | 'pending' | 'failed' | 'refunded';
  billingCycle: 'monthly' | 'quarterly' | 'yearly';
  unitPrice: number;
  quantity: number;
  totalAmount: number;
  currency: string;
  inheritFromTenant: boolean; // 是否继承租户订阅
  createdAt: string;
  updatedAt: string;
  // 关联信息
  plan?: SubscriptionPlan;
  organization?: {
    id: string;
    name: string;
    code: string;
  };
}

// 套餐查询过滤器
export interface SubscriptionPlanFilter {
  type?: PlanType;
  status?: Status;
  isDefault?: boolean;
  isVisible?: boolean;
  validDate?: string; // 查询指定日期有效的套餐
}

// 订阅查询过滤器
export interface SubscriptionFilter {
  tenantId?: string;
  organizationId?: string;
  planId?: string;
  status?: string;
  paymentStatus?: string;
  expiring?: boolean; // 即将到期
  expiringDays?: number; // 多少天内到期
}

// 套餐表单数据
export interface SubscriptionPlanFormData {
  name: string;
  code: string;
  type: PlanType;
  description?: string;
  features: DetailedPlanFeature[];
  limits: PlanLimits;
  pricing: PlanPricing[];
  status: Status;
  isDefault: boolean;
  isVisible: boolean;
  displayOrder: number;
  validFrom?: string;
  validTo?: string;
}

// 订阅表单数据
export interface SubscriptionFormData {
  planId: string;
  startDate: string;
  endDate: string;
  autoRenew: boolean;
  billingCycle: 'monthly' | 'quarterly' | 'yearly';
  quantity: number;
}

// 升级/降级订阅请求
export interface UpgradeSubscriptionRequest {
  subscriptionId: string;
  newPlanId: string;
  effectiveDate: string;
  prorated: boolean; // 是否按比例计费
}

// 续费订阅请求
export interface RenewSubscriptionRequest {
  subscriptionId: string;
  billingCycle: 'monthly' | 'quarterly' | 'yearly';
  quantity?: number;
}

// 创建订阅套餐请求
export interface CreateSubscriptionPlanRequest {
  name: string;
  price: number;
  billingCycle: 'monthly' | 'yearly';
  maxUsers: number;
  maxOrganizations: number;
  maxStorage: number;
  features?: string[];
  description?: string;
}

// 更新订阅套餐请求
export interface UpdateSubscriptionPlanRequest extends Partial<CreateSubscriptionPlanRequest> {}

// 套餐对比
export interface PlanComparison {
  features: {
    name: string;
    code: string;
    plans: Record<string, boolean | string | number>;
  }[];
  limits: {
    name: string;
    code: string;
    plans: Record<string, number | boolean>;
  }[];
}